package net.phoenix.repository;

import java.util.ArrayList;
import java.util.List;

import javax.jcr.AccessDeniedException;
import javax.jcr.Item;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;

import net.phoenix.repository.iterator.BasicPropertyIterator;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-2
 * @version 1.0.0
 */
public abstract class GeneralNode implements Node {

	public GeneralNode() {
	}


	/**
	 * 当前node是否为parent的儿子/孙子节点
	 * 
	 * @param parent
	 *            Node
	 * @return boolean
	 * @throws RepositoryException
	 * @throws ItemNotFoundException
	 * @throws AccessDeniedException
	 */
	public boolean isChild(Node parent) throws RepositoryException {
		Node temp = this.getParent();
		while (temp != null) {
			if (temp.getIdentifier().equals(parent.getIdentifier()))
				return true;
			temp = temp.getParent();
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Node#getProperties(java.lang.String)
	 */
	@Override
	public PropertyIterator getProperties(String namePattern) throws RepositoryException {
		List<Property> result = new ArrayList<Property>();
		for (PropertyIterator iterator = this.getProperties(); iterator.hasNext();) {
			Property property = iterator.nextProperty();
			if (property.getName().matches(namePattern))
				result.add(property);
		}
		return new BasicPropertyIterator(result);
	}

	@Override
	public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
		List<Property> result = new ArrayList<Property>();
		for (PropertyIterator iterator = this.getProperties(); iterator.hasNext();) {
			Property property = iterator.nextProperty();
			boolean match = false;
			for (int i = 0; i < nameGlobs.length && !match; i++) {
				if (property.getName().matches(nameGlobs[i]))
					match = true;
			}
			if (match)
				result.add(property);
		}
		return new BasicPropertyIterator(result);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Node#getPrimaryItem()
	 */
	@Override
	public Item getPrimaryItem() throws ItemNotFoundException, RepositoryException {

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Node#getUUID()
	 */
	@Override
	public String getUUID() throws UnsupportedRepositoryOperationException, RepositoryException {
		return this.getIdentifier();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (obj instanceof Node)
			try {
				return this.getIdentifier().equals(((Node) obj).getIdentifier());
			} catch (RepositoryException e) {
				return false;
			}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Item#isNode()
	 */
	@Override
	public boolean isNode() {
		return true;
	}

	/* (non-Javadoc)
	 * @see javax.jcr.Item#getDepth()
	 */
	@Override
	public int getDepth() throws RepositoryException {
		if (this.getParent() == null)
			return 0;
		return this.getParent().getDepth() + 1;
	}
	
	
}
